<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card>
      <div>
        <el-row :gutter="20">
          <el-col :span="9"><div class="grid-content bg-purple">
            <el-input clearable placeholder="请输入内容" v-model="queryInfo.query" @clear="getGoodsList">
              <el-button slot="append" icon="el-icon-search" @click="getGoodsList"></el-button>
            </el-input></div></el-col>
          <el-col :span="6"><div class="grid-content bg-purple">
            <el-button type="primary" @click="goAddGood">添加商品</el-button>
          </div></el-col>
        </el-row>
      </div>
      <!-- 用户列表区域 -->
      <el-table
        border
        stripe
        :data="goodsList">
        <el-table-column
          type="index"
          label="#">
        </el-table-column>
        <el-table-column
          prop="goods_name"
          label="商品名称">
        </el-table-column>
        <el-table-column
          width="120px"
          prop="goods_price"
          label="商品价格(元)">
        </el-table-column>
        <el-table-column
          width="90px"
          prop="goods_weight"
          label="商品重量">
        </el-table-column>
        <el-table-column
          width="160px"
          label="创建时间">
          <template slot-scope="scope">
            {{ scope.row.add_time | dateFormat }}
          </template>
        </el-table-column>
        <el-table-column
          width="130px"
          label="操作">
          <template
            slot-scope="scope">
            <el-tooltip effect="dark" content="修改" placement="top" :enterable="false">
              <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            </el-tooltip>
            <el-tooltip effect="dark" content="删除" placement="top" :enterable="false">
              <el-button type="danger" @click="delGood(scope.row.goods_id)" icon="el-icon-delete" size="mini"></el-button>
            </el-tooltip>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[4, 10, 25, 50]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 10
      },
      goodsList: [],
      total: 0
    }
  },
  created() {
    this.getGoodsList()
  },
  methods: {
    async getGoodsList() {
      const { data: res } = await this.$http.get('goods', { params: this.queryInfo })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.goodsList = res.data.goods
      this.total = res.data.total
    },
    // 每页有多少条数据
    handleSizeChange(countsize) {
      this.queryInfo.pagesize = countsize
      this.getGoodsList()
    },
    // 当前页码
    handleCurrentChange(currentChange) {
      this.queryInfo.pagenum = currentChange
      this.getGoodsList()
    },
    // 删除商品
    async delGood(id) {
      const confirmRestul = await this.$confirm('此操作将永久删除该商品, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      // 点击确定按钮confirm
      // 点击取消cancel
      if (confirmRestul === 'cancel') return this.$message.info('已取消删除')
      const { data: res } = await this.$http.delete(`goods/${id}`)
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.$message.success(res.meta.msg)
      this.getGoodsList()
    },
    // 跳转到添加商品页面
    goAddGood() {
      this.$router.push('goods/add')
    }
  }
}
</script>

<style lang="less" scoped>
</style>
